<?php
//数据按照树结构的要求进行排序
function authtree($auth_menu){
    return resort($auth_menu);
}
function resort($data,$parentid=0,$level=0)
{
    static $ret=array();
    foreach ($data as $k => $v) {
        if(is_object($v)) {
            $arr = (array)$v;
            if($arr['pid']==$parentid){
                $arr['level']=$level;
                $ret[]=$arr;
                resort($data,$arr['id'],$level+1);
            }
        }
    }
    return $ret;
}
function has_child($id)
{
    $rs = DB::select("select count(*) c from zhyl_sys_auth where auth_pid=?",[$id]);
    $aa= sqlcount($rs);
    return sqlcount($rs)>0??false;
}

/**
 * @param 查数量
 * @return mixed
 */
function sqlcount($rs){
    return $rs[0]->c;
}


/**
 * 格式化打印数组
 */
function p($arr)
{
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
}

/*
 * 数组写入文件
 * @param $data 数据源
 * @param $type 1单条 2数组
 */
function wlog($data,$type=1){
    if($type==1){
        $file = fopen("single.txt", 'w');
        fwrite($file, $data);
        fclose($file);
    }else if($type==2){
        $fp = fopen('test_arr.txt', 'w+');
        fwrite($fp, var_export($data, true));
        fclose($fp);
    }
}

/**
 * @param $ids
 * 将数组转换成字符串返回，用语sql中的in 使用
 */
function implode_arrs($ids){
    return "'".implode("','", $ids)."'";
}

function returnMsg(){
    return json_encode(array("msg"=>"success","flag"=>1,"data"=>""));
}
function returnFail(){
    return json_encode(array("msg"=>"fail","flag"=>0,"data"=>""));
}
function uploadfile($request){
    $up_file = $request->file;
    $ext = $up_file->getClientOriginalExtension();
    $realpath = $up_file->getRealPath();
    $file = uniqid().'.'.$ext;
    $bool = Storage::disk('uploads')->put($file,file_get_contents($realpath));
    return $file;
}

function getSession($request){
    return $request->session()->get('userinfo')[0];
}

//生成短信验证码
function generate_code($length = 6)
{
    $min = pow(10, ($length - 1));
    $max = pow(10, $length) - 1;
    return rand($min, $max);
}
function send_post($url, $post_data)
{

    $postdata = http_build_query($post_data);
    $options  = array('http' => array('method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60 // 超时时间（单位:s）
    ));
    $context  = stream_context_create($options);
    $result   = file_get_contents($url, false, $context);

    return $result;
}
/**
 * 统一返回模板
 */
function return_msg($data,$flag,$msg){
    $res = array("data"=>$data,"flag"=>$flag,"msg"=>$msg);
    return $res;
}

